package com.song.shiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.shiro.entity.Permission;
import com.song.shiro.entity.Role;
import com.song.shiro.entity.User;
import com.song.shiro.mapper.UserMapper;
import com.song.shiro.service.PermissionService;
import com.song.shiro.service.RoleService;
import com.song.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public User queryByUsername(String username) {
        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getUsername, username);
        return getOne(lambda);
    }

    @Override
    public List<Role> queryRolesByUsername(String username) {
        User user = queryByUsername(username);
        if (user == null){
            return new ArrayList<>();
        }
        return roleService.queryListByUserId(user.getId());
    }

    @Override
    public List<Permission> queryPermissionsByUsername(String username) {
        User user = queryByUsername(username);
        if (user == null){
            return new ArrayList<>();
        }
        return permissionService.queryListByUserId(user.getId());
    }

    @Override
    public List<Role> queryRolesByUserId(String userId) {
        return roleService.queryListByUserId(userId);
    }

    @Override
    public List<Permission> queryPermissionsByUserId(String userId) {
        return permissionService.queryListByUserId(userId);
    }
}
